package com.csii.user.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.core.api.Result;
import com.csii.user.entity.OauthClientDetails;
import com.csii.user.service.IOauthClientDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author ian
* @since 2020-07-09
*/
@Controller
@RequestMapping("/sys/oauthClientDetails")
@Api(tags="authClient")
public class OauthClientDetailsController {
    @Autowired
    protected IOauthClientDetailsService service;
    private static final String HTML_PREFIX ="sys/oauthClientDetails/";

    @GetMapping(value={"/", "list",""})
    public String oauthClientDetails(){
       return HTML_PREFIX + "list";
    }



    @PostMapping("/page")
    @ResponseBody
    @ApiOperation(value = "列表")
    public Result page(Page page, OauthClientDetails entry) {
        return Result.ok(service.page(page, Wrappers.lambdaQuery(OauthClientDetails.class).eq(entry!=null&& StringUtils.isNotEmpty(entry.getClientId()),OauthClientDetails::getClientId,entry==null?"":entry.getClientId())) );
    }

/**
     * 跳转新增或修改页面
     * @param id
     * @param model
     * @return
     */

    @GetMapping(value={"/form", "/form/{id}"})
    public String form(@PathVariable(required = false) String id, Model model) {
        OauthClientDetails entity;
        if(id==null||id.isEmpty())
            return HTML_PREFIX + "add";
        else
            entity =service.getById(id);
        model.addAttribute("oauthClientDetails", entity);
        return HTML_PREFIX + "form";
    }


    @RequestMapping(value="add",method = RequestMethod.PUT)
    public String add(OauthClientDetails entry){
        service.save(entry);
        return "redirect:/"+HTML_PREFIX;
    }


    @RequestMapping(value="update",method = RequestMethod.PUT)
    public String update(OauthClientDetails entry){
         service.updateById(entry);
        return "redirect:/"+HTML_PREFIX;
    }


    @DeleteMapping(value="del/{id}")
    @ResponseBody
    @ApiOperation(value = "删除")
    public Result del(@PathVariable(value = "id") String id){
        service.removeById(id);
        return Result.ok("删除成功");
    }
}
